/**
 * 作者:hanbo
 * 日期:2018/11/1915:37
 * 描述:地图视图
 */
import 'jquery';
import {LayerManager} from "./LayerManager";
import {EventCenter} from "./EventCenter";
import {MapRender} from "./MapRender";
import {GLStateCenter} from "./GLStateCenter";
import {InteractiveEvent} from "./InteractiveEvent";
import {MapControl} from "./MapControl";

export class MapView {

    constructor(id) {

        let container = $("#" + id);
        //检测地图容器是否存在
        if (container.length) {
            //存在则初始化
            this._id = id;
            this.minLevel = 0;
            this.maxLevel = 17;
            this.gLStateCenter = new GLStateCenter(this);
            this._interactiveEvent = new InteractiveEvent(this);
            this._mapRender = new MapRender(this, id);
            this._layerManager = new LayerManager(this);
            this.mapControl=new MapControl(this);
            this._eventCenter = new EventCenter(this);

        } else {
            //不存在则报错
            console.error("id不正确");
        }
    }

    _onMapReady() {
        const that = this;
        let handler = setInterval(function () {
            if (that._eventCenter) {
                that._eventCenter.onMapReady();
                that.gLStateCenter.setLevel(0);
                clearInterval(handler)
            }
        }, 30)
    }

    /**
     * 设置地图缩放等级
     * @param level {number} 缩放等级
     */
    setMapLevel(level) {
        if (level >= this.minLevel && level <= this.maxLevel) {
            this.gLStateCenter.setLevel(level);
        }
    }
}